package com.service.rd;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.excel.EasyExcel;
import com.common.StringUtils;
import com.common.easyui.EasyuiUtils;
import com.common.http.HttpResult;
import com.common.http.HttpStatus;
import com.dao.rd.RdOrderDao;
import com.dao.rd.RdProgramMapper;
import com.dao.rd.RdSupplyDealDao;
import com.model.purchase.PurchaseSupplyDeal;
import com.model.rd.OrderModel;
import com.model.rd.Program;
import com.model.rd.SupplyDealModel;
import com.common.model.Global;

@Service("rdOrderService")
public class OrderService {
	static final Logger logger = LoggerFactory.getLogger(OrderService.class);

	@Autowired
	RdOrderDao orderDao;
	@Autowired
	RdSupplyDealDao supplyDealDao;
	@Autowired
	RdProgramMapper programMapper;

	/**
	 * 列表查询
	 *
	 * @param queryMap
	 * @return
	 */
	public HttpResult selectList(Map<String, Object> queryMap) {
		HttpResult httpResult = new HttpResult();
		EasyuiUtils.addPageNum(queryMap);
		EasyuiUtils.addFilter(queryMap);
		List<OrderModel> list = orderDao.selectList(queryMap);
		int count = orderDao.selectListCount(queryMap);
		httpResult.setRows(list);
		httpResult.setTotal(count);
		logger.info("selectList:{}", list);
		return httpResult;
	}

	/**
	 * 根据采购方案生成采购订单
	 * 
	 * @param gramId
	 * @return
	 */
	@Transactional
	public HttpResult createOrder(String gramId) {
		if (!StringUtils.isNotNull(gramId)) {
			return HttpResult.error("参数不能为空！");
		}
		int result = 0;
		HttpResult httpResult = new HttpResult();
		HashMap<String, Object> hashMap = new HashMap<String, Object>();
		hashMap.put("gramId", gramId);
		Program program = programMapper.load(gramId);
		List<SupplyDealModel> supplyDeals = supplyDealDao.selectList(hashMap);
		for (SupplyDealModel supplyDeal : supplyDeals) {
			// 生成采购订单
			OrderModel orderModel = new OrderModel();
			orderModel.setOrdId(UUID.randomUUID().toString());

			orderModel.setGramId(gramId);
			orderModel.setResId(gramId);
			orderModel.setResCode(program.getGramCode());
			orderModel.setInfId(supplyDeal.getInfId());
			orderModel.setInfCode(supplyDeal.getInfCode());
			orderModel.setInfName(supplyDeal.getInfName());
			orderModel.setOrdFreight(supplyDeal.getInfLastTotalPrice());
			orderModel.setResName(program.getGramName());
			orderModel.setOrdContractName(supplyDeal.getInfContact());
			result += orderDao.insert(orderModel);
		}
		program.setStatus(Global.NUMERICAL_TWO + "");
		programMapper.update(program);
		
		if (result > 0) {
			httpResult.setMsg("生成成功！");
		} else {
			httpResult.setMsg("生成失败！");
			httpResult.setCode(HttpStatus.GLOBAL_EXCEPTION_CODE);
		}
		return httpResult;
	}

	/**
	 * 详情
	 * 
	 * @param
	 * @return
	 */
	public HttpResult selectInfor(String infId, String gramId) {
		HttpResult httpResult = new HttpResult();
		OrderModel orderModel = orderDao.load(infId, gramId);
		httpResult.setData(orderModel);
		httpResult.setCode(HttpStatus.SUCCESS_CODE);
		return httpResult;
	}

	/**
	 * 保存
	 *
	 * @param
	 * @return
	 */
	public HttpResult save(OrderModel orderModel) {
		if (orderModel == null) {
			return HttpResult.error("参数不能为空");
		}
		HttpResult httpResult = new HttpResult();
		int result = 0;
		if (!StringUtils.isNotNull(orderModel.getInfId())) {
			orderModel.setInfId(UUID.randomUUID().toString());
			result = orderDao.insert(orderModel);
		} else {
			result = orderDao.update(orderModel);
		}
		if (!StringUtils.isNotNull(orderModel.getGramId())) {
			orderModel.setGramId(UUID.randomUUID().toString());
			result = orderDao.insert(orderModel);
		} else {
			result = orderDao.update(orderModel);
		}
		if (result > 0) {
			httpResult.setMsg(orderModel.getStatus().equals(Global.YES + "") ? "提交成功!" : "保存成功!");
			httpResult.setData(orderModel);
		} else {
			httpResult.setCode(HttpStatus.GLOBAL_EXCEPTION_CODE);
			httpResult.setMsg(orderModel.getStatus().equals(Global.YES + "") ? "提交失败!" : "保存失败!");
		}
		return httpResult;
	}

	/**
	 * 删除
	 *
	 * @param
	 * @return
	 */
	public HttpResult delete(String infId, String gramId) {
		HttpResult httpResult = new HttpResult();
		if (!StringUtils.isNotNull(infId) || !StringUtils.isNotNull(gramId)) {
			return HttpResult.error("参数不能为空");
		}
		int result = orderDao.delete(infId, gramId);
		if (result > 0) {
			httpResult.setMsg("删除成功！");
		} else {
			httpResult.setCode(HttpStatus.GLOBAL_EXCEPTION_CODE);
			httpResult.setMsg("不存在此条数据！");
		}
		return httpResult;
	}

	/**
	 * 导出
	 * 
	 * @param queryMap
	 * @param response
	 */
	public void export(Map<String, Object> queryMap, HttpServletResponse response) {
		List<OrderModel> list = orderDao.selectList(queryMap);
		logger.info("export:{}", list);
		try {
			EasyExcel.write(response.getOutputStream(), OrderModel.class).sheet("模板").doWrite(list);
		} catch (IOException e) {
			logger.error("导出错误:{}", e.getMessage());
		}

	}
}
